package com.qingxin.sys.action;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.entity.WxUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.WxUserService;
import com.qingxin.sys.support.SystemCfg;
import com.qingxin.sys.utils.StringUtils;


@Controller
@RequestMapping("/wxuser")
public class WxUserAction extends BaseAction {

	@Autowired
	private WxUserService wxUserService;
	
	
	/**
	 * 用户列表
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "sys/wxuser/wxuser_list";
	}
	
	
	/**
	 * 列表数据
	 * @param search
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(String search, PageParam pageParam, HttpServletRequest request){
		LoginUser loginUser = getLoginUser(request);
		if(!SystemCfg.isAdminUser(loginUser)){
			return new Pager();
		}
		return wxUserService.list(search, pageParam, request);
	}
	
	
	/**
	 * 添加用户
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, HttpServletRequest request){
		return "sys/wxuser/wxuser_add";
	}
	
	
	/**
	 * 保存用户信息
	 * @param user
	 * @param result
	 * @param repasswd
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method=RequestMethod.POST)
	@ResponseBody
	public Object save(@Valid WxUser user, BindingResult result, String repasswd, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		if(StringUtils.isEmpty(user.getPasswd()) || !user.getPasswd().equals(repasswd)){
			return new SysError("passwd", "密码输入错误");
		}
		
		SysError info = wxUserService.add(user, request);
		return info;
	}

	
	/**
	 * 修改用户信息
	 * @param model
	 * @param wxuserid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String wxuserid, HttpServletRequest request){
		WxUser user = wxUserService.get(wxuserid);
		model.put("user", user);
		return "sys/wxuser/wxuser_edit";
	}
	
	
	/**
	 * 保存修改的用户信息
	 * @param user
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid WxUser user, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		SysError info = wxUserService.update(user, request);
		return info;
	}
	
	
	/**
	 * 查看用户详细详细
	 * @param model
	 * @param wxuserid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/view.do", method=RequestMethod.GET)
	public String view(ModelMap model, String wxuserid, HttpServletRequest request){
		WxUser user = wxUserService.get(wxuserid);
		model.put("user", user);
		return "sys/wxuser/wxuser_view";
		
	}
	
	
	/**
	 * 删除用户账号
	 * @param wxuserid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] wxuserid, HttpServletRequest request){
		if(wxuserid==null || wxuserid.length==0){
			return new SysError("没有选择任何信息.");
		}
		return wxUserService.delete(wxuserid, request);
	}
	
	
	/**
	 * 重置密码
	 * @param wxuserid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/resetPassword.json", method=RequestMethod.POST)
	@ResponseBody
	public Object resetPassword(String wxuserid, HttpServletRequest request){
		if(StringUtils.isEmpty(wxuserid)){
			return new SysError("参数错误.");
		}
		return wxUserService.resetPassword(wxuserid, null, request);
	}
	
	
	/**
	 * 判断用户账号是否存在
	 * @param loginName
	 * @return 0不存在、1已存在
	 */
	@RequestMapping(value="/isLoginnameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isLoginnameDuplicate(String loginName){
		WxUser user = wxUserService.getByProperty("loginName", loginName);
		return (user==null) ? "0":"1";
	}
	
	
	/**
	 * 判断用户手机号是否存在
	 * @param phone
	 * @param wxuserid
	 * @return 0不存在、1已存在
	 */
	@RequestMapping(value="/isPhoneDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isPhoneDuplicate(String phone, String wxuserid){
		boolean duplicate = wxUserService.isPhoneDuplicate(phone, wxuserid);
		return duplicate ? "1":"0";
	}
	
	
}
